package com.timothy.question;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.timothy.android.util.XMLUtil;

public class QuestionBuilder {
	private final static String LOG_TAG = "QuestionBuilder";
	/**
	 * 
	 * @param content
	 * @return
	 */
	public static List<BaseStation> parseBaseStation(String content) {
		List<BaseStation> bsList = new ArrayList<BaseStation>();
		List<String> list = XMLUtil.getChildren(content);
		for (String s : list) {
			BaseStation bs = new BaseStation();
			bs.setName(XMLUtil.getPropertyValue(s, "Name"));
			bsList.add(bs);
		}
		return bsList;
	}
	
	/**
	 *    <Message Type="CommonQuestion">
	 *	    <Content>
	 *	      <Question Type="1" QuestionName="Are you question?" SlideIndex="1">
	 *	        <Choice Index="1" Text="Yes"/>
	 *	        <Choice Index="2" Text="No"/>
	 *	      </Question>      
	 *	    </Content>
	 *	  </Message>
	 * @param quesiton
	 * @return
	 */
/*	public static STCommonButtonAlpha parseSTTwoButtonAlpha( String quesiton ) {
		STCommonButtonAlpha q = new STCommonButtonAlpha();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			c.setMark( XMLUtil.getPropertyValue(choice, "Index") );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		return q;
	}*/

	/**
	 *    <Message Type="CommonQuestion">
	 *	    <Content>
	 *	      <Question Type="2" QuestionName="What is your phone size?" SlideIndex="2">
	 *	        <Choice Index="1" Text="Small"/>
	 *	        <Choice Index="2" Text="Normal"/>
	 *	        <Choice Index="3" Text="Large"/>
	 *	      </Question>      
	 *	    </Content>
  	 *	</Message>
	 */
	public static STCommonButtonAlpha parseSTThreeButtonAlpha( String quesiton ) {
		STCommonButtonAlpha q = new STCommonButtonAlpha();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			c.setMark( XMLUtil.getPropertyValue(choice, "Index") );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		
		return q;
	}
	
	/**
	 * 
	 * <Message Type="CommonQuestion">
	 * <Content>
	 * 	<Question Type="n" QuestionName="What is your grade?" SlideIndex="3">
	 * 		<Choice Index="1" Text="1"/>
	 * 		<Choice Index="2" Text="2"/>
	 * 		.......
	 * 		<Choice Index="N" Text="N"/>
	 * </Question>
	 * </Content>
	 * </Message>
	 * 
	 * @param quesiton
	 * @return
	 */
	public static STCommonButtonAlpha parseSTButtonQuestion( String quesiton ) {
		STCommonButtonAlpha q = new STCommonButtonAlpha();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			c.setMark( XMLUtil.getPropertyValue(choice, "Index") );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		
		return q;
	}
	
	/**
	 *   <Message Type="CommonQuestion">
	 *		  <Content>
	 *	      <Question Type="multi" QuestionName="which food blow you like?" SlideIndex="4">
	 *		        <Choice Index="1" Text="Fish"/>
	 *		        <Choice Index="2" Text="Beef"/>
	 *		        <Choice Index="3" Text="Pork"/>
	 *		        <Choice Index="4" Text="Egg"/>
	 *	      </Question>      
	 *	    </Content>
	 *	  </Message>
	 * @param quesiton
	 * @return
	 */
	public static STCommonButtonAlpha parseMultiSelect( String quesiton ) {
		STCommonButtonAlpha q = new STCommonButtonAlpha();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			c.setMark( XMLUtil.getPropertyValue(choice, "Index") );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		
		return q;
	}
	
	/**
	 *   <Message Type="CommonQuestion">
	 *   <Content>
	 *   <Question Type="slide" QuestionName="How old are you?" SlideIndex="5">
	 *   <Min>0</Min>
	 *   <Max>100</Max>
	 *   </Question>     
	 *   </Content>
	 *   </Message>
	 * Hi tang qi, this way is right
	 * @param quesiton
	 * @return
	 */
	public static SlideQuestion parseSlideQuestion( String quesiton ) {
		SlideQuestion q = new SlideQuestion();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex(Integer.valueOf(slideIndex));
		q.setQuestionName(questName);
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> lines = XMLUtil.getChildren(quesiton);

		//set min
		String minLine = lines.get(0);//<Max>100</Max>
		Log.i(LOG_TAG,"minLine:" + minLine);
		Integer min= Integer.valueOf(XMLUtil.getElementValue(minLine)) ;
		Log.i(LOG_TAG,"min:" + min);
		
		//set max
		String maxLine = lines.get(1);//<Max>100</Max>
		Log.i(LOG_TAG,"maxLine:" + maxLine);
		Integer max = Integer.valueOf(XMLUtil.getElementValue(maxLine)) ;
		Log.i(LOG_TAG,"max:" + max);
		
		q.setMax(max);
		return q;
	}

	/**
	 * <Message Type="CommonQuestion"> 
	 * <Content> 
	 * <Question Type="priority" QuestionName="Which sport you like" SlideIndex="6"> 
	 * <Choice Index="1" Text="Basketball"/> 
	 * <Choice Index="2" Text="Football"/> 
	 * <Choice Index="3" Text="Swimming"/> 
	 * <Choice Index="4" Text="Tennis"/> 
	 * <Choice Index="5" Text="Badmintain"/> 
	 * <Choice Index="6" Text="Golf"/> 
	 * </Question>
	 * </Content> 
	 * </Message>
	 * @param quesiton
	 * @return
	 */
	public static PriorityQuestion parsePriorityQuestion( String quesiton ) {
		PriorityQuestion q = new PriorityQuestion();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			String index = XMLUtil.getPropertyValue(choice, "Index");
			c.setIndex(Integer.valueOf(index));
			c.setMark( index );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		return q;
	}
	
	/**
	 *  <Message Type="CommonQuestion">
     *		<Content>
	 *    		<Question Type="freeForm" QuestionName="Which sport you like" SlideIndex="6">
	 *     		</Question>      
     *		</Content>
  	 *	</Message>
	 * @param quesiton
	 * @return
	 */
	public static FreeformQuestion parseFreeformQuestion( String quesiton ) {
		FreeformQuestion q = new FreeformQuestion();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex(Integer.valueOf(slideIndex));
		q.setQuestionName(questName);
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		return q;
	}

	/**
	 *  <Message Type="CommonQuestion">
     *		<Content>
	 *    		<Question Type="freeForm" QuestionName="Which sport you like" SlideIndex="6">
	 *     		</Question>      
     *		</Content>
  	 *	</Message>
	 * @param quesiton
	 * @return
	 */
	public static RangeQuestion parseRangeQuestion( String quesiton ) {
		RangeQuestion q = new RangeQuestion();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex(Integer.valueOf(slideIndex));
		q.setQuestionName(questName);
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		return q;
	}

	/**
	 * <Message Type="CommonQuestion"> 
	 * <Content> 
	 * 	<Question Type="grading" QuestionName="Please input scores of courses" VotingWeight="270" TeamCode="1" SlideIndex="6"> 
	 * 		<Choice Index="1" Text="Chinese" Min="0" Max="100"/> 
	 * 		<Choice Index="2" Text="Math" Min="0" Max="100"/> 
	 * 		<Choice Index="3" Text="English" Min="0" Max="100"/> 
	 * 	</Question> 
	 * 	</Content>
	 * </Message>
	 * @param quesiton
	 * @return
	 */
	public static GradingQuestion parseGradingQuestion( String quesiton ) {
		GradingQuestion q = new GradingQuestion();
		String slideIndex = XMLUtil.getPropertyValue( quesiton, "SlideIndex");
		String questName = XMLUtil.getPropertyValue( quesiton, "QuestionName");
		String countDownTime = XMLUtil.getPropertyValue( quesiton, "CountDownTime");
		
		q.setSlideIndex( Integer.valueOf(slideIndex) );
		q.setQuestionName( questName );
		q.setCountDownTime( Integer.valueOf(countDownTime) );
		
		List<String> choices = XMLUtil.getChildren( quesiton );
		for( String choice : choices ) {
			Choice c = new Choice();
			c.setMark( XMLUtil.getPropertyValue(choice, "Index") );
			c.setContent( XMLUtil.getPropertyValue(choice, "Text") );
			q.addChoice( c ); 
		}
		
		q.setTeamCode("A");//todo
		q.setScoreWeight(270);//todo
		
		return q;
	}
	
}
